/**
 * jims
 */
package com.jims.master.modules.nurse.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.nurse.entity.BedRec;
import com.jims.master.modules.nurse.service.BedRecService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 床位管理Controller
 *
 * @author 赵宁
 * @version 2017-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/nurse/bedRec")
public class BedRecController extends BaseController {

    @Autowired
    private BedRecService bedRecService;

    @ModelAttribute
    public BedRec get(@RequestParam(required = false) String id) {
        BedRec entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = bedRecService.get(id);
        }
        if (entity == null) {
            entity = new BedRec();
        }
        return entity;
    }

    /**
     * 床位维护列表
     *
     * @param bedRec
     * @param model
     * @return
     * @author 陈涛
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "formList")
    public String formList(BedRec bedRec, HttpServletRequest request, HttpServletResponse response, Model model) {
        String orgDeptId = UserUtils.getUser().getOrgDept().getId();
        bedRec.setDeptId(orgDeptId);
        bedRec.setWordCode(orgDeptId);
        bedRec.setOrgId(UserUtils.getOrgId());
        Page<BedRec> page = bedRecService.findPage(new Page<BedRec>(request, response), bedRec);
        model.addAttribute("page", page);
        return "modules/nurse/bedRecList";
    }

    /**
     * 包床列表
     * @param model
     * @return
     * @author 陈涛
     * @update_by CTQ AT 2017-05-11 14:16:35
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "bedOccupyList")
    public String bedOccupyList( String visitNo, Model model) {
        model.addAttribute("visitNo", visitNo);
        return "modules/nurse/bedOccupy";
    }

    /**
     * @param bedRec 传递参数
     * @return String    返回类型
     * @throws
     * @Title: bedOccupyLeft
     * @Dscription: (加载床位病人列表)
     * @author CTQ
     * @date 2017-05-11
     */
    @RequestMapping(value = "bedOccupyLeft")
    public String bedOccupyLeft(BedRec bedRec, Model model) {
        bedRec.setOrgId(UserUtils.getOrgId());
        String orgDeptId = UserUtils.getUser().getOrgDept().getId();
        bedRec.setDeptId(orgDeptId);
        bedRec.setWordCode(orgDeptId);
        //没有被包出去床位标识为0
        bedRec.setBedOccupy("0");
        //有病人床位列表
        List<BedRec> inBedList = bedRecService.inBedList(bedRec);

        model.addAttribute("inBedList", inBedList);
        return "modules/nurse/bedOccupyLeft";
    }

    /**
     * @param bedRec 传递参数
     * @return String    返回类型
     * @throws
     * @Title: bedOccupyRight
     * @Dscription: (加载病人已包床和空床列表)
     * @author CTQ
     * @date 2017-05-11
     */
    @RequestMapping(value = "bedOccupyRight")
    public String bedOccupyRight(BedRec bedRec, String visitNo, Model model) {
        bedRec.setOrgId(UserUtils.getOrgId());
        String orgDeptId = UserUtils.getUser().getOrgDept().getId();
        bedRec.setDeptId(orgDeptId);
        bedRec.setWordCode(orgDeptId);
        //没有被包出去床位标识为0
        bedRec.setBedOccupy("0");
        bedRec.setBedStatus("1");
        //可供包床的空床位列表
        List<BedRec> notInBedList = bedRecService.notInBedList(bedRec);
        BedRec bedRec1 = new BedRec();
        bedRec1.setOrgId(UserUtils.getOrgId());
        //已包床位标识为病人住院号
        bedRec1.setBedOccupy(visitNo);
        bedRec1.setDeptId(orgDeptId);
        bedRec1.setWordCode(orgDeptId);
        //已包床位列表
        List<BedRec> inBedRecsVisitNo = bedRecService.notInBedList(bedRec1);
        model.addAttribute("visitNo", visitNo);
        model.addAttribute("bedRec", bedRec);
        model.addAttribute("notInBedList", notInBedList);
        model.addAttribute("inBedRecsVisitNo", inBedRecsVisitNo);
        return "modules/nurse/bedOccupyRight";
    }

    /**
     * 包床
     * @param bedRec
     * @return
     * @update_by CTQ
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveInBed")
    @ResponseBody
    public StringData saveInBed(BedRec bedRec, String[] ids) {
        StringData data = new StringData();
        int i = bedRecService.updateBedOccupy(ids,bedRec);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存床位管理成功");
        } else {
            data.setCode("error");
            data.setData("保存床位管理失败");
        }
        return data;
    }


    /**
     * 床位添加修改页面
     *
     * @param bedRec
     * @param model
     * @return
     * @author 陈涛
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(BedRec bedRec, Model model) {
        model.addAttribute("bedRec", bedRec);
        return "modules/nurse/bedRecForm";
    }

    /**
     * 换床
     * @param bedRec
     * @param model
     * @return
     * @author 陈涛
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "umbetten")
    @ResponseBody
    public StringData umbetten(BedRec bedRec, Model model, Integer bedNoNew, String bedLabelNew) {
        StringData data = new StringData();
        String orgDeptId = UserUtils.getUser().getOrgDept().getId();
        int i = 0;
        BedRec bedRecNew = new BedRec();
        bedRecNew.setBedNo(bedNoNew);
        bedRecNew.setBedLabel(bedLabelNew);
        bedRecNew.setWordCode(orgDeptId);
        bedRecNew.setDeptId(orgDeptId);
        //检查是否有这个床位
        bedRecNew = bedRecService.getByBedNo(bedRecNew);
        if (bedRecNew == null) {
            i = -1;
        } else {
            String visitIdNew = bedRecNew.getVisitId();
            //这个床位上是否有病人
            if (visitIdNew == null || visitIdNew.equals("")) {
                i = bedRecService.umbetten(bedRec, bedRecNew);
            } else {
                i = -2;
            }
        }

        if (i > 0) {
            data.setCode("success");
            data.setData("保存床位管理成功");
        } else {
            if (i == -1) {
                data.setCode("error");
                data.setData("床号与床标号不存在");
            } else if (i == -2) {
                data.setCode("error");
                data.setData("此床位有病人");
            } else {
                data.setCode("error");
                data.setData("保存床位管理失败");
            }
        }
        return data;
    }

    /**
     * 验证床位号是否重复
     * @param bedRec
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "validIsExit")
    @ResponseBody
    public StringData validIsExit(BedRec bedRec) {
        StringData data = new StringData();
        if (StringUtils.isBlank(bedRec.getId())){
            bedRec.setWordCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
            List<BedRec> rec = bedRecService.findbedRecNo(bedRec);
            if (rec!=null&&rec.size()>0){
                data.setCode("error");
                data.setData("床号["+bedRec.getBedNo()+"]已存在，不能添加重复床号");
            }else{
                data.setCode("success");
                data.setData("保存床位管理成功");
            }
        }
        return data;
    }

    /**
     * 床位添加修改
     * @param bedRec
     * @param model
     * @return
     * @author 陈涛
     * @update_by CTQ AT 2017-05-11 14:17:04
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(BedRec bedRec, Model model) {
        StringData data = new StringData();
        int i = 0;
        i = bedRecService.saveBedRec(bedRec);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存床位管理成功");
        } else {
            data.setCode("error");
            data.setData("保存床位管理失败");
        }
        return data;
    }

    /**
     * 床位信息删除
     *
     * @param bedRec
     * @return
     * @author 陈涛
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(BedRec bedRec) {
        StringData data = new StringData();
        int i = 0;
        String visitId = bedRec.getVisitId();
        if (visitId != null && visitId != "") {
            i = -3;
        } else {
            i = bedRecService.delete(bedRec);
        }
        if (i > 0) {
            data.setCode("success");
            data.setData("删除床位管理成功");
        } else {
            if (i == -3) {
                data.setCode("error");
                data.setData("此床有病人不能删除");
            } else {
                data.setCode("error");
                data.setData("删除床位管理失败");
            }
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"autoComplete", ""})
    @ResponseBody
    public List<BedRec> autoComplete(String q, String limit) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        BedRec bedRec = new BedRec();
        bedRec.setWordCode(UserUtils.getUser().getOrgDept().getId());
        bedRec.updateOrgId();
        return bedRecService.findEmptyBedList(q, limit, bedRec);
    }

    /**
     * @param bedRec 传递参数
     * @return StringData    返回类型
    * @throws
     * @Title: validBedOccupy
     * @Dscription: (验证患者是否有包床)
     * @author CTQ
     * @date 2017-05-12
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "validBedOccupy")
    @ResponseBody
    public StringData validBedOccupy(BedRec bedRec) {
        StringData data = new StringData();
        int i = 0;
        bedRec.updateOrgId();
        i = bedRecService.validBedOccupy(bedRec);
        if (i > 1) {
            data.setCode("info");
            data.setData("该病人有包床，请先解除该病人的包床后再进行操作！");
        } else {
            data.setCode("success");
        }
        return data;
    }

    /**
     * 根据房间号获取最大床号
     * @param bedRec
     * @return
     * @author CTQ
     * @date 2017-07-26 21:55:05
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findMaxBedNo")
    @ResponseBody
    public String findMaxBedNo(BedRec bedRec) {
        bedRec.updateOrgId();
        bedRec.setWordCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
        int i = bedRecService.findMaxBedNo(bedRec);
        return String.valueOf(i);
    }


}